package dao

import (
	"context"
	"github.com/redis/go-redis/v9"
	"log"
	"time"
)

var Rdb *redis.Client

func InitRedis() {
	Rdb = redis.NewClient(&redis.Options{
		Addr:     "shrewdboy.top:6379",
		Password: "2662029447",
		DB:       1, //  DB 1
	})

	ctx, channel := context.WithTimeout(context.Background(), 5*time.Second)
	defer channel()

	pong, err := Rdb.Ping(ctx).Result()
	if err != nil {
		log.Println("Failed to connect to Redis:", err)
	}
	log.Println("Connected to Redis:", pong)
}

func Set(key string, value interface{}, expiration time.Duration) error {
	ctx := context.Background()
	return Rdb.Set(ctx, key, value, expiration).Err()
}

func Get(key string) (string, error) {
	ctx := context.Background()
	val, err := Rdb.Get(ctx, key).Result()
	if err != nil {
		return "", err
	}
	return val, nil
}
